//
//  HKAudioTool.swift
//  HKUtilitySwift
//
//  Created by 胡锦涛 on 2018/12/27.
//  Copyright © 2018 胡锦涛. All rights reserved.
//

// 这个类, 负责, 单首歌曲的操作, 播放, 暂停, 停止, 快进, 倍数
import UIKit
import AVFoundation

class HKAudioTool: NSObject {
    var player: AVAudioPlayer?
    
    
    func playMusic(musicName: String?) -> () {
        
        
        // 1. 创建播放器
        guard let url = Bundle.main.url(forResource: musicName, withExtension: nil) else {return}
        
        if player?.url == url {
            player?.play()
            return
        }
        
        
        do {
            player = try AVAudioPlayer(contentsOf: url)
            player?.prepareToPlay()
            player?.play()
        }catch {
            print(error)
            return
        }
        
        
    }
    
    func pauseMusic() -> () {
        player?.pause()
    }
    
    ///获取正确的时间格式
    class func getFormatTime(timeInterval: TimeInterval) -> String {
        
        // timeInterval 21.123
        let min = Int(timeInterval) / 60
        let sec = Int(timeInterval) % 60
        return String(format: "%02d: %02d", min, sec)
    }
    
    ///获取音频列表Models
    /*
     class func getMusicMs(result: ([QQMusicModel])->()) {
     
     // 解析歌曲信息
     // 1. 获取文件路径
     guard let path = Bundle.main.path(forResource: "Musics.plist", ofType: nil) else {
     result([QQMusicModel]())
     return
     }
     
     // 2. 读取文件内容
     guard let array = NSArray(contentsOfFile: path) else {
     result([QQMusicModel]())
     return
     }
     
     
     // 3. 解析: 转换成歌曲模型
     var models = [QQMusicModel]()
     for dic in array {
     let model = QQMusicModel(dic: dic as! [String : AnyObject])
     models.append(model)
     }
     
     // 4. 返回结果
     result(models)
     
     }
     */
}
